package com.infore.exchange.main.controller;

import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.exchange.TJobInfo;
import com.infore.common.model.entity.exchange.TJobTaskInfo;
import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiResponse;
import com.infore.common.utils.UUIDGenerator;
import com.infore.exchange.main.quartz.CronTaskRegistrar;
import com.infore.exchange.main.quartz.SchedulingRunnable;
import com.infore.exchange.main.service.QuartzTaskService;
import com.infore.exchange.main.service.TJobInfoService;
import com.infore.exchange.main.service.TJobTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 任务调度 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-02-13
 */

@Slf4j
@RestController
@Api(value = "任务调度前端控制器", tags = "任务调度前端控制器")
@RequestMapping("/quartz")
public class QuartzController extends BaseController {

    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    @Autowired
    private TJobTaskInfoService TJobTaskInfoService;

    @Autowired
    private TJobInfoService TJobInfoService;

    @Autowired
    private QuartzTaskService quartzTaskService;

    @ResponseBody
    @ApiOperation(value = "作业绑定任务", notes = "作业绑定任务")
    @RequestMapping(value="/addOrEdit",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ApiEntityResponse<TJobInfo> addOrEdit(@RequestParam  @ApiParam(name = "jobId",required = true,value = "作业ID") String  jobId,
                                                 @RequestParam  @ApiParam(name = "taskIdsStr",required = true,value = "任务IDS,用,隔开") String  taskIdsStr){
        TJobInfo jobInfo = TJobInfoService.selectById(jobId);
        //清空之前绑定的数据
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("job_id", jobId);
        TJobTaskInfoService.deleteByMap(columnMap);
        List<String> taskIds= Arrays.asList(taskIdsStr.split(","));
        List<TJobTaskInfo> list = new ArrayList<>();
        for (String taskId:taskIds) {
            TJobTaskInfo entity = new TJobTaskInfo();
            entity.setId(UUIDGenerator.generator());
            entity.setJobId(jobId);
            entity.setTaskId(taskId);
            list.add(entity);
        }
        //重新插入数据
        boolean insertOk = TJobTaskInfoService.insertBatch(list);
        if(insertOk){
            SchedulingRunnable task = new SchedulingRunnable(jobInfo.getBeanName(), jobInfo.getMethodName(), jobInfo.getJobId());
            if(!cronTaskRegistrar.hasCronTask(task)){
                cronTaskRegistrar.addCronTask(task, jobInfo.getCronSchedule());
            }
        }
        return ApiEntityResponse.ok(jobInfo);
    }

    @ResponseBody
    @ApiOperation(value = "暂停或启动作业", notes = "暂停或启动作业")
    @RequestMapping(value="/changesStatus",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ApiEntityResponse<TJobInfo>  changesStatus(@RequestParam   @ApiParam(name = "jobId",required = true,value = "作业ID") String  jobId,
                                                      @RequestParam   @ApiParam(name = "status",required = true,value = "状态") Integer status) {

        TJobInfo jobInfo = TJobInfoService.selectById(jobId);
        boolean ok = TJobInfoService.updateJobStateById(jobId, status);
        if(ok){
            if(status.equals(1)){
                //添加任务
                log.info("启动任务\t作业ID="+jobId);
                SchedulingRunnable task = new SchedulingRunnable(jobInfo.getBeanName(), jobInfo.getMethodName(), jobInfo.getJobId());
                if(!cronTaskRegistrar.hasCronTask(task)){
                    cronTaskRegistrar.addCronTask(task, jobInfo.getCronSchedule());
                }
            }else{
                //清除任务
                log.info("暂停任务\t作业ID="+jobId);
                SchedulingRunnable task = new SchedulingRunnable(jobInfo.getBeanName(), jobInfo.getMethodName(), jobInfo.getJobId());
                if(cronTaskRegistrar.hasCronTask(task)){
                    cronTaskRegistrar.removeCronTask(task);
                }
            }

        }
        return ApiEntityResponse.ok(jobInfo);
    }
    @ResponseBody
    @ApiOperation(value = "手动执行", notes = "手动执行")
    @RequestMapping(value="/execution",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ApiResponse changesStatus(@RequestParam   @ApiParam(name = "jobId",required = true,value = "作业ID") String  jobId) {
        quartzTaskService.rdbmsDataExchang(jobId);
        return ApiResponse.ok();
    }

}
